package com.sskj.functions.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.hjq.toast.ToastUtils;
import com.sskj.common.base.App;
import com.sskj.common.box.inputfilter.MoneyValueFilter;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.functions.R;
import com.sskj.functions.R2;
import com.sskj.functions.presenter.PledgePresenter;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.CurrentTypeBean;
import com.sskj.lib.bean.PledgeBean;
import com.sskj.lib.bean.PledgeInterestBean;
import com.sskj.lib.util.ArithUtil;
import com.sskj.lib.util.CommonUtil;
import com.sskj.lib.widget.BottomDialog;
import com.sskj.munite.MinuteView;
import com.zzhoujay.richtext.RichText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/06/26
 */
@Route(path = RConfig.FUNCTIONS_DYJB)
public class PledgeActivity extends BaseActivity<PledgePresenter> {
    List<String> coins = new ArrayList();

    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.pledge_bg)
    Button pledgeBg;
    @BindView(R2.id.pledge_choose_rl)
    LinearLayout pledgeChooseRl;
    @BindView(R2.id.pledge_borrow_rl)
    LinearLayout pledgeBorrowRl;
    @BindView(R2.id.pledge_time_rl)
    LinearLayout pledgeTimeRl;
    @BindView(R2.id.pledge_choose_tv)
    TextView pledgeChooseTv;
    @BindView(R2.id.pledge_borrow_tv)
    TextView pledgeBorrowTv;
    @BindView(R2.id.pledge_time_tv)
    TextView pledgeTimeTv;
    @BindView(R2.id.pledge_pledge_number)
    EditText pledgePledgeNumber;
    @BindView(R2.id.pledge_shift_number)
    EditText pledgeShiftNumber;
    @BindView(R2.id.pledge_expire)
    TextView pledgeExpire;
    @BindView(R2.id.pledge_borrow_money)
    TextView pledgeBorrowMoney;
    @BindView(R2.id.pledge_interest_rate)
    TextView pledgeInterestRate;
    @BindView(R2.id.pledge_expire_state)
    TextView pledgeExpireState;
    @BindView(R2.id.pledge_borrow_money_state)
    TextView pledgeBorrowMoneyState;
    @BindView(R2.id.tvBili)
    TextView tvBili;
    @BindView(R2.id.tvTian)
    TextView tvTian;
    @BindView(R2.id.tvContent)
    TextView tvContent;
    @BindView(R2.id.et_rate)
    EditText etRate;
    private String state = "BTC";
    private String pledgeStateOne = "usdt";
    private String pledgeStateTwo = "btc";
    private String procedures = "";
    private String languages = "1";
    private List<String> taday = new ArrayList<>();

    private String lilv;//利率
    private String shouxufei;//手续费

    private String rateMin;//最小借贷利率
    private String rateMax;//最大借贷利率

    @Override
    public int getLayoutId() {
        return R.layout.functions_activity_pledge;
    }

    @Override
    public PledgePresenter getPresenter() {
        return new PledgePresenter();
    }

    @Override
    public void initView() {
        mPresenter.getCurrentType();
        etRate.setFilters(new InputFilter[]{new MoneyValueFilter().setDigits(6)});

        if (Constans.LANGUAGE_EN.equals(SPUtil.get(SPConfig.LANGUAGE, Constans.LANGUAGE_CNA))) {
            languages = "2";
        } else if (Constans.LANGUAGE_CNA.equals(SPUtil.get(SPConfig.LANGUAGE, Constans.LANGUAGE_CNA))) {
            languages = "1";
        } else if (Constans.LANGUAGE_TRA.equals(SPUtil.get(SPConfig.LANGUAGE, Constans.LANGUAGE_CNA))) {
            languages = "3";
        }
        mPresenter.getTimeData(languages);
        tvTitle.setText(App.INSTANCE.getString(R.string.functions_pledgeActivity1));
        pledgeShiftNumber.setFilters(new InputFilter[]{new MoneyValueFilter().setDigits(8)});

        initEditWatcher();

    }

    private void initEditWatcher() {
        pledgeShiftNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (isEmpty(s.toString())) {
                    pledgeExpire.setText("0.00");
                    return;
                }
                if (!isEmpty(getText(etRate)) && !isEmpty(getText(pledgeTimeTv))) {
                    double mul = ArithUtil.mul(ArithUtil.mul(Double.valueOf(getText(etRate)), Double.valueOf(s.toString())), Double.valueOf(getText(pledgeTimeTv)));
//                    double mul = ArithUtil.mul(Double.valueOf(getText(etRate)), Double.valueOf(s.toString()));
                    try {
                        pledgeExpire.setText(CommonUtil.dealCoinNum(String.valueOf(ArithUtil.div(mul, 3000, 8)), getText(pledgeBorrowTv)));
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
                if (!isEmpty(shouxufei) && !isEmpty(getText(pledgeTimeTv))) {
                    double mul = ArithUtil.mul(ArithUtil.mul(Double.valueOf(shouxufei), Double.valueOf(s.toString())), Double.valueOf(getText(pledgeTimeTv)));
                    try {
                        pledgeBorrowMoney.setText(CommonUtil.dealCoinNum(String.valueOf(ArithUtil.div(mul, 3000, 8)), getText(pledgeBorrowTv)));
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        etRate.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (isEmpty(s.toString()) || isEmpty(getText(pledgeTimeTv))) {
                    pledgeExpire.setText("0.00");
                    return;
                }
                if (!isEmpty(getText(pledgeShiftNumber))) {
                    double mul = ArithUtil.mul(ArithUtil.mul(Double.valueOf(getText(pledgeShiftNumber)), Double.valueOf(s.toString())), Double.valueOf(getText(pledgeTimeTv)));
                    try {
                        pledgeExpire.setText(CommonUtil.dealCoinNum(String.valueOf(ArithUtil.div(mul, 3000, 8)), getText(pledgeBorrowTv)));
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    @Override
    public void initData() {
        //立即借币
        ClickUtil.click(300, pledgeBg, () -> {
            if (isEmpty(getText(pledgePledgeNumber))) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.functions_pledgeActivity4));
                return;
            }
            if (isEmpty(pledgeShiftNumber)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.functions_pledgeActivity3));
                return;
            }
            if (isEmpty(getText(etRate))) {
                ToastUtils.show(App.INSTANCE.getString(R.string.qingshurujiedaililv));
                return;
            }
            if (!isEmpty(rateMin)) {
                if (Double.valueOf(rateMin) > Double.valueOf(getText(etRate))) {
                    ToastUtils.show(App.INSTANCE.getString(R.string.jiedaililvbunengdiyu) + rateMin + "%");
                    return;
                }
            }
            if (!isEmpty(rateMax)) {
                if (Double.valueOf(rateMax) < Double.valueOf(getText(etRate))) {
                    ToastUtils.show(App.INSTANCE.getString(R.string.jiedaililvbunenggaoyu) + rateMax + "%");
                    return;
                }
            }
            if (isEmpty(pledgeTimeTv)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.functions_pledgeActivity2));
                return;
            }
            BottomDialog.posswordInput(this, (tv) -> {
                mPresenter.startPledge(pledgePledgeNumber.getText().toString()
                        , getText(pledgeChooseTv).toLowerCase()
                        , pledgeShiftNumber.getText().toString()
                        , getText(pledgeBorrowTv).toLowerCase()
                        , pledgeTimeTv.getText().toString()
                        , getText(etRate)
                        , shouxufei
                        , pledgeExpire.getText().toString()
                        , pledgeBorrowMoney.getText().toString()
                        , tv);
            });
        });

        ClickUtil.click(300, this.pledgeChooseRl, () -> {
            if (coins.size() == 0) {
                ToastUtils.show(App.INSTANCE.getString(R.string.walletFunctions_currentTreasureBizhong));
                return;
            }
            BottomDialog.chooseCoins(this, this.coins, (tv) -> {
                this.pledgeChooseTv.setText(tv);
                pledgeStateOne = tv.toLowerCase();
                pledgeBorrowMoneyState.setText(state);
            });
        });

        ClickUtil.click(300, this.pledgeBorrowRl, () -> {
            if (coins.size() == 0) {
                ToastUtils.show(App.INSTANCE.getString(R.string.walletFunctions_currentTreasureBizhong));
                return;
            }
            BottomDialog.chooseCoins(this, this.coins, (tv) -> {
                this.pledgeBorrowTv.setText(tv);
                pledgeShiftNumber.getText().clear();
                if (!pledgeTimeTv.getText().toString().equals("")) {
                    mPresenter.getPledgeInterest(pledgeTimeTv.getText().toString());
                }
                pledgeStateTwo = tv.toLowerCase();
                state = tv;
                pledgeExpireState.setText(tv);
                pledgeBorrowMoneyState.setText(tv);
            });
        });
        ClickUtil.click(300, this.pledgeTimeRl, () -> {
            if (taday.size() == 0) {
                ToastUtils.show(App.INSTANCE.getString(R.string.functions_pledgeActivity5));
            }
            BottomDialog.chooseCoins(this, taday, (tv) -> {
                this.pledgeTimeTv.setText(tv);
                mPresenter.getPledgeInterest(tv);
            });
        });
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, PledgeActivity.class);
        context.startActivity(intent);
    }

    @SuppressLint("SetTextI18n")
    public void getPledgeInterestSuccess(PledgeInterestBean data) {
        if (data != null) {
            rateMin = data.getInterest_rate_range_one();
            rateMax = data.getInterest_rate_range_two();
            etRate.setHint(App.INSTANCE.getString(R.string.lilvfanwei) + rateMin + "-" + rateMax);
            tvBili.setText(data.getInterest());
            lilv = data.getInterest();
            shouxufei = data.getProcedures();


            if (isEmpty(getText(pledgeShiftNumber)) || isEmpty(getText(etRate))) {
                pledgeExpire.setText("0.00");
            } else {
                double mul = ArithUtil.mul(ArithUtil.mul(Double.valueOf(getText(etRate)), Double.valueOf(getText(pledgeShiftNumber))), Double.valueOf(getText(pledgeTimeTv)));
                try {
                    pledgeExpire.setText(CommonUtil.dealCoinNum(String.valueOf(ArithUtil.div(mul, 3000, 8)), getText(pledgeBorrowTv)));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }

            if (isEmpty(shouxufei) || isEmpty(pledgeTimeTv) || isEmpty(getText(pledgeShiftNumber))) {
                pledgeBorrowMoney.setText("0.00");
            } else {
                double mul = ArithUtil.mul(ArithUtil.mul(Double.valueOf(shouxufei), Double.valueOf(getText(pledgeShiftNumber))), Double.valueOf(getText(pledgeTimeTv)));
                try {
                    pledgeBorrowMoney.setText(CommonUtil.dealCoinNum(String.valueOf(ArithUtil.div(mul, 3000, 8)), getText(pledgeBorrowTv)));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }


//            if (isEmpty(getText(pledgeShiftNumber))) {
//                return;
//            }
//            double mul = ArithUtil.mul(Double.valueOf(lilv), Double.valueOf(getText(pledgeShiftNumber)));
//            try {
//                pledgeExpire.setText(CommonUtil.dealCoinNum(String.valueOf(ArithUtil.div(mul, 100, 8)), getText(pledgeBorrowTv)));
//            } catch (IllegalAccessException e) {
//                e.printStackTrace();
//            }
//            double mul1 = ArithUtil.mul(Double.valueOf(shouxufei), Double.valueOf(getText(pledgeShiftNumber)));
//            try {
//                pledgeBorrowMoney.setText(CommonUtil.dealCoinNum(String.valueOf(ArithUtil.div(mul1, 100, 8)), getText(pledgeBorrowTv)));
//            } catch (IllegalAccessException e) {
//                e.printStackTrace();
//            }
        }
    }

    public void getCurrentTypeSuccess(List<String> data) {
        if (data == null) {
            return;
        }
        coins.addAll(data);
    }

    /*抵押借币天数及借币说明*/
    public void getPledgeData(PledgeBean data) {
        if (data == null) {
            return;
        }
        RichText.from(data.getContent()).into(tvContent);
        if (data.getTaday() == null) {
            return;
        }
        taday = data.getTaday();
        if (taday.size() > 0) {
            pledgeTimeTv.setText(taday.get(0));
            mPresenter.getPledgeInterest(taday.get(0));
        }
    }
}
